/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.digitalization.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.entity.B01Entity;
import org.springblade.modules.digitalization.service.IB01Service;
import org.springblade.modules.digitalization.vo.B01VO;
import org.springblade.modules.digitalization.wrapper.B01Wrapper;
import org.springblade.modules.system.entity.Dict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 单位基本情况信息集-JAVA 控制器
 *
 * @author RS
 * @since 2022-10-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("damanager/b01")
@Api(value = "单位基本情况信息集-JAVA", tags = "单位基本情况信息集-JAVA接口")
public class B01Controller extends BladeController {

	private final IB01Service b01Service;

	/**
	 * 单位基本情况信息集-JAVA 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入b01")
	public R<B01VO> detail(B01Entity b01) {
		B01Entity detail = b01Service.getOne(Condition.getQueryWrapper(b01));
		return R.data(B01Wrapper.build().entityVO(detail));
	}
	/**
	 * 单位基本情况信息集-JAVA 树列表
	 */
	/**
	 * 分页 单位基本情况信息集
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入b01")
	public R<IPage<B01VO>> list(B01Entity daB01, Query query) {
		IPage<B01Entity> pages = b01Service.page(Condition.getPage(query), Condition.getQueryWrapper(daB01).orderByAsc("SORTID"));
		return R.data(B01Wrapper.build().pageVO(pages));
	}

	/**
	 * 单位基本情况信息集-JAVA 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入b01")
	public R<IPage<B01VO>> page(B01VO b01, Query query) {
		IPage<B01VO> pages = b01Service.selectB01Page(Condition.getPage(query), b01);
		return R.data(pages);
	}

	/**
	 * 单位基本情况信息集-JAVA 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入b01")
	public R save(@Valid @RequestBody B01Entity b01) {
		return R.status(b01Service.save(b01));
	}

	/**
	 * 单位基本情况信息集-JAVA 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入b01")
	public R update(@Valid @RequestBody B01Entity b01) {
		return R.status(b01Service.update(b01));
	}

	@PostMapping("/savesort")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入b01")
	public R saveSort(@Valid @RequestBody List<B01Entity> b01s) {
//		TODO:新增单位逻辑
		return R.status(b01Service.updateBatchById(b01s));
	}

	/**
	 * 单位基本情况信息集-JAVA 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入b01")
	public R submit(@Valid @RequestBody B01Entity b01) {
		return R.status(b01Service.saveOrUpdate(b01));
	}

	/**
	 * 单位基本情况信息集-JAVA 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(b01Service.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 单位基本情况信息集-JAVA 树形结构
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<B01VO>> tree(String tenantId,
							   @RequestParam(value = "withAuth", defaultValue = "false") Boolean withAuth,
							   BladeUser bladeUser) {
		if (withAuth) {
			List<B01VO> tree = b01Service.treeWithAuth(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
			return R.data(tree);
		} else {
			List<B01VO> tree = b01Service.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
			return R.data(tree);
		}

	}

	@GetMapping("/path")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "路径", notes = "根据单位名称获取完整路径")
	public R<List<B01Entity>> path(String tenantId, String deptCode, BladeUser bladeUser) {
		tenantId = Func.toStrWithEmpty(tenantId, bladeUser.getTenantId());
		List<B01Entity> path = b01Service.path(tenantId, deptCode);
		return R.data(path);
	}

	@PostMapping("/b01upload")
	@ApiLog("机构树导入")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "机构树导入", notes = "")
	public R b01upload(@RequestParam(value = "file", required = false) MultipartFile file) {
		R r = b01Service.upload(file);
		return r;
	}

	@GetMapping("/dictionary")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取字典", notes = "获取字典")
	public R<List<B01VO>> dictionary() {
		List<B01VO> tree = b01Service.dictory();
		return R.data(tree);
	}

}
